package bieche.eric.beans;
import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import bieche.eric.util.*;

public class EventBean implements Serializable
{
   /**
    * 
    */
   private static final long serialVersionUID = -8712177145236881976L;
   // This will get set by the faces-config.xml via injection
   private FormBean formBean;
   public void setFormBean(FormBean fb) { this.formBean = fb; }
   public FormBean getFormBean()        { return this.formBean; }

   public void addPlayer(ActionEvent ae)
   {
      formBean.getPlayers().add( new TribblePlayer(formBean.getName()) );
      System.out.println("Added Player : #" + formBean.getName() + "#");
   }

   public void doneAddPlayer(ActionEvent ae)
   {
      formBean.getPlayers().add( new TribblePlayer(formBean.getName()) );
      System.out.println("Added Player : #" + formBean.getName() + "#");
   }

   public void toggleAddNamesPopup(ActionEvent event)
   {
      System.out.println("rendered tf = " + formBean.isAddNamesPopupRendered());
      formBean.setAddNamesPopupRendered( ! formBean.isAddNamesPopupRendered() );
   }
   
   public void validatePlayerNameBeforeAdd(FacesContext context, UIComponent component, Object value)
   {
      for(TribblePlayer tp : formBean.getPlayers())
      {
         if(tp.getName().equals( formBean.getName() ))
         {
            String msg = "Error: Trying to add the same name please change.";
            System.out.println("id = " + component.getId());
            System.out.println("value = " + (String)value);
            context.addMessage(component.getId(), new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg));
            //context.addMessage("dataEntry", new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg));
            return;
         }
      }
   }
}
